package cn.xiaolang.function.file_manage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

import cn.xiaolang.common.utils.Monitor;


public class ReadFromDocument {

    public static void main(String[] args) {

        String fileAbsolutePath = "C:\\Users\\fuhw@go-goal.com\\Desktop\\汇总.xlsx";
        File file = new File(fileAbsolutePath);
        readByBufferedReader(file);
        System.gc();
        readByFileInputStream(file);

    }

    /**
     * 用FileInputSteam读取文件（保持文本文件的原样）
     *
     * @param file
     * @author fuhw
     * @date 2016年6月6日 上午11:04:19
     */
    private static void readByFileInputStream(File file) {
        Monitor.begin();
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] buff = new byte[1024];
            int len = 0;
            StringBuffer sbResult = new StringBuffer();
            while ((len = fileInputStream.read(buff)) > 0) {
                sbResult.append(new String(buff, 0, len));
            }
            Monitor.end("用FileInputStream读取文件数据完成用时：");
            System.out.println(sbResult.length());
            fileInputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用BufferedReader读取文件（按行读，拼在了一起）
     *
     * @param file
     */
    private static void readByBufferedReader(File file) {
        Monitor.begin();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String lineString = null;
            StringBuffer sbResult = new StringBuffer();
            while ((lineString = bufferedReader.readLine()) != null) {
                sbResult.append(lineString);
            }
            Monitor.end("BufferedReader读取文件数据完成用时：");
            System.out.println(sbResult.length());
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
